#include <bits/stdc++.h>
using namespace std;
long long a[50];


int main()
{
	int x,n;
	cin >> x >> n;
	a[0] = 1;
	for (int i = 1; a[i-1] < 1000000000; i++)
	{
		a[i] = a[i-1] * x;
	}
	while(n--)
	{
		int m;
		scanf("%d",&m);
		int i = 0,x = 0;
		for (i = 0; a[i] <= m; i++)
		{}
		x = m - a[i-1];
		printf("%d\n",x); 
	}
	return 0;
}
